<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/2
 * Time: 14:08
 */

namespace app\common\controller;


use app\common\model\CardMember;
use Wechat\Wxobjopen;

class CardMarke extends Wxoption
{

    //激活会员卡
    public function activcardus($arr)
    {

        $CardMember = new CardMember();
        $card = $CardMember->where(['card_id' => $arr['card_id']])->field('bonus_rule,is_pay_balance,price')->find();
        $bonus_rule = json_decode($card['bonus_rule'], true);
        $arr['init_bonus'] = $bonus_rule['init_increase_bonus'];
        $options = $this->options($arr['card_id']);
        $wxObj = new Wxobjopen($options);
        $data['card_id'] = $arr['card_id'];
        $data['code'] = $arr['code'];

        $res = $wxObj->cardFire($data);
        $this->int_activuser($arr);
            if($card['is_pay_balance']==1){
            $arrs2['code']=$arr['code'];
            $arrs2['card_id']=$arr['card_id'];
            $arrs2['price']=$card['price'];
            $this->init_blance($arrs2);
        }
        return $res;
    }

    //调用更新数据，写入填写信息和初始数据
    public function int_activuser($arr)
    {

        $data['cardid'] = $arr['card_id'];
        $data['usercardcode'] = $arr['code'];
        $data['openid'] = $arr['openid'];
        $data['createtime'] = time();
        $res = action('index/Wxcard/userfirecard', array($data));
//        file_put_contents('/data2/html/payos/test.txt', 'wxpay-29' . json_encode($res, JSON_UNESCAPED_UNICODE), FILE_APPEND);
        return $res;
    }

    //开卡送余额
    public function init_blance($arr)
    {
       $Balance=new Balance();
       $add=$Balance->updatebalance($arr);
       return $add;

    }

    //初始化积分余额
    public function init_bonus($arr)
    {
//    try {
        //实例化微信
        $options = $this->options($arr['card_id']);
        $WxObj = new Wxobjopen($options);
        $data = [
            'code' => $arr['code'],
            'card_id' => $arr['card_id'],
            'record_bonus' => 0,       //说明
//            'add_bonus' => 0,             //积分变动值  负数为减少
            'custom_field_value2' => $arr['price'] . '元',
            'notify_optional' => [
                'is_notify_bonus' => false,
                'is_notify_balance' => false,
                'is_notify_custom_field2' => false,
            ],
        ];
        $res = $WxObj->updateUserInfo($data);
//    } catch (\Exception $e) {
//        $res = ['errcode' => 9999, 'message' => $e->getMessage()];
//    }
        return $res;
    }


}